package com.scpii.universal.util;

import java.nio.ByteBuffer;

public class CacheUtil {

	public static final String getCachekey(int cmd, byte[] data) {
		
		int len = (data != null && data.length > 0 ? data.length : 0) + 4 + 4;
		byte[] sendbuf = new byte[len];
		h2n_32(sendbuf, len, 0);
		h2n_32(sendbuf, cmd, 4);
		if(data != null && data.length > 0)
		System.arraycopy(data, 0, sendbuf, 8, data.length);
		return MD5.strMD5(sendbuf);
	}
	
	private static final void h2n_32(byte[] dest, int i, int offset)
	{
		ByteBuffer bb = ByteBuffer.allocate(4);
		bb.putInt(i);
		byte[] b = bb.array();
		dest[offset] = b[0];
		dest[offset + 1] = b[1];
		dest[offset + 2] = b[2];
		dest[offset + 3] = b[3];
	}
	
}
